﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Order
{
    class Program
    {
        static OrderService orderService = new OrderService();
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("输入数字以进行相应操作。\n1.添加订单 2.删除订单 3.修改订单 4.查询订单 5.退出");
                switch (GetCommand())
                {
                    case "1": Add(); break;
                    case "2": Del(); break;
                    case "3": Modify(); break;
                    case "4": Inquiry(); break;
                    case "5": return;
                }
            }
        }
        static string GetCommand()
        {
            string[] vs;
            do
            {
                vs = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            } while (vs.Length == 0);
            return vs[0];
        }
        static void Add()
        {
            Order newOrder;
            do
            {
                Console.WriteLine("输入订单号:");
                newOrder = new Order(Console.ReadLine().Trim());
                try
                {
                    orderService.InquiryOrders(newOrder);
                    Console.WriteLine("订单号已存在。");
                    return;
                }
                catch
                {
                    break;
                }
            } while (true);
            Console.WriteLine("输入客户名：");
            newOrder.Customer.Name = Console.ReadLine().Trim();
            int year = 0, month = 0, day = 0, size = 0, commodityNum = 0;
            bool loop;
            do
            {
                try
                {
                    Console.WriteLine("输入订单年份：");
                    year = int.Parse(Console.ReadLine().Trim());
                    if (year < 0) throw new ArgumentException();
                    loop = false;
                }
                catch
                {
                    loop = true;
                }
            } while (loop);
            do
            {
                try
                {
                    Console.WriteLine("输入订单月份：");
                    month = int.Parse(Console.ReadLine().Trim());
                    if (month < 1 || month > 12) throw new ArgumentException();
                    loop = false;
                }
                catch
                {
                    loop = true;
                }
            } while (loop);
            do
            {
                try
                {
                    Console.WriteLine("输入订单日：");
                    day = int.Parse(Console.ReadLine().Trim());
                    newOrder.OrderTime = new DateTime(year, month, day);
                    loop = false;
                }
                catch
                {
                    loop = true;
                }
            } while (loop);
            Console.WriteLine("输入收货地址：");
            newOrder.ShippingAddress.Location = Console.ReadLine().Trim();
            do
            {
                try
                {
                    Console.WriteLine("输入订单明细数：");
                    size = int.Parse(Console.ReadLine().Trim());
                    newOrder.OrderDetails = new OrderDetail[size];
                    for(int i = 0; i < size; i++)
                    {
                        newOrder.OrderDetails[i] = new OrderDetail();
                    }
                    loop = false;
                }
                catch
                {
                    loop = true;
                }
            } while (loop);
            for (int i = 0; i < size; i++)
            {
                do
                {
                    try
                    {
                        Console.WriteLine("输入货物数：");
                        commodityNum = int.Parse(Console.ReadLine().Trim());
                        newOrder.OrderDetails[i].Commodities = new Commodity[commodityNum];
                        for(int j = 0; j < commodityNum; j++)
                        {
                            newOrder.OrderDetails[i].Commodities[j] = new Commodity();
                        }
                        loop = false;
                    }
                    catch
                    {
                        loop = true;
                    }
                } while (loop);
                for (int j = 0; j < commodityNum; j++)
                {
                    Console.WriteLine("输入货物名：");
                    newOrder.OrderDetails[i].Commodities[j].Name = Console.ReadLine().Trim();
                    do
                    {
                        try
                        {
                            Console.WriteLine("输入单价：");
                            newOrder.OrderDetails[i].Commodities[j].Price = int.Parse(Console.ReadLine().Trim());
                            loop = false;
                        }
                        catch
                        {
                            loop = true;
                        }
                    } while (loop);
                }
            }
            newOrder.ComputeTotal();
            orderService.AddOrder(newOrder);
        }
        static void Del()
        {
            Console.WriteLine("输入订单号:");
            try
            {
                orderService.DeleteOrder(Console.ReadLine().Trim());
            }
            catch (KeyNotFoundException e)
            {
                Console.WriteLine($"The given {e.Message} not found.");
            }
        }
        static void Modify()
        {
            List<Order> list = Inquiry();
            if (list == null) return;
            foreach (var o in list)
            {
                orderService.DeleteOrder(o);
            }
            Add();
        }
        static List<Order> Inquiry()
        {
            OrderService.InquiryType type;
            string command = "";
            double total = 0;
            List<Order> list = null;
            do
            {
                Console.WriteLine("通过\n1.订单号 2.商品名称 3.客户 4.订单金额\n查询: ");
                switch (GetCommand())
                {
                    case "1":
                        type = OrderService.InquiryType.OrderID;
                        break;
                    case "2":
                        type = OrderService.InquiryType.CommodityName;
                        break;
                    case "3":
                        type = OrderService.InquiryType.CustomerName;
                        break;
                    case "4":
                        type = OrderService.InquiryType.Total;
                        break;
                    default:
                        continue;
                }
                break;
            } while (true);
            switch (type)
            {
                case OrderService.InquiryType.OrderID:
                    Console.WriteLine("输入订单号:");
                    command = Console.ReadLine().Trim();
                    break;
                case OrderService.InquiryType.CommodityName:
                    Console.WriteLine("输入商品名:");
                    command = Console.ReadLine().Trim();
                    break;
                case OrderService.InquiryType.CustomerName:
                    Console.WriteLine("输入客户名：");
                    command = Console.ReadLine().Trim();
                    break;
                case OrderService.InquiryType.Total:
                    do
                    {
                        try
                        {
                            Console.WriteLine("输入订单金额：");
                            total = double.Parse(Console.ReadLine().Trim());
                            break;
                        }
                        catch { }
                    } while (true);
                    break;
            }
            try
            {
                switch (type)
                {
                    case OrderService.InquiryType.CommodityName:
                    case OrderService.InquiryType.CustomerName:
                    case OrderService.InquiryType.OrderID:
                        list = orderService.InquiryOrders(command, type);
                        break;
                    case OrderService.InquiryType.Total:
                        list = orderService.InquiryOrders(total);
                        break;
                }
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("未找到符合条件的Order。");
                return null;
            }
            StringBuilder builder = new StringBuilder();
            foreach (var item in list)
            {
                builder.Append(item.ToString());
                builder.Append('\n');
            }
            Console.WriteLine(builder.ToString(0, builder.Length - 1));
            return list;
        }
    }
}
